﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using IBatisNet.Common.Utilities;
using IBatisNet.DataAccess;
using IBatisNet.DataAccess.Configuration;
using K.Persistence.MapperDao;
using System.DirectoryServices;

namespace K.Service
{
    class ServiceConfig
    {
        static private object _synRoot = new object(); //实例化一个静态位置对象？？
        static private ServiceConfig _instance;        //定义一个静态空的ServiceConfig(自身)变量
        private IDaoManager _daoManager = null;         //顶一个空的DaoManager变量

        //空的构造函数
        private ServiceConfig()
        { 
        
        }

        /// <summary>
        /// 获取唯一实例，这里采用了单例模式，并采取双重锁定以确保线程安全
        /// </summary>
        /// <returns></returns>
        static public ServiceConfig GetInstance()
        {
            if (_instance == null)
            {
                lock (_synRoot) //锁定对象，设置一个临界区。
                {
                    if (_instance == null)
                    {
                        
                        
                        ConfigureHandler handler = new ConfigureHandler(ServiceConfig.Reset);
                        try
                        {
                            DomDaoManagerBuilder domDaoManagerBuilder = new DomDaoManagerBuilder();
                            string path = Path.GetTempFileName();
                            //非Web程序
                            //FileInfo fi1 = new FileInfo(System.Environment.CurrentDirectory + "\\dao.config");
                            //Web程序
                            FileInfo fi1 = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\dao.config");

                            /*
                             * //以下为测试代码
                            string str2 = Environment.CurrentDirectory;//获取和设置当前目录（即该进程从中启动的目录）的完全限定路径。 
                            string str3 = Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。 
                            string str4 = AppDomain.CurrentDomain.BaseDirectory;//获取基目录，它由程序集冲突解决程序用来探测程序集。 
                            string str7 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。
                            */


                            //domDaoManagerBuilder.ConfigureAndWatch(handler); //监控配置文件是否发生变化
                            domDaoManagerBuilder.Configure(fi1);
                            //DaoManager.ConfigureAndWatch(handler);  //此方法已过期
                        }
                        catch(Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }

                        //DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
                        //builder.Configure();


                        //取得实例

                        _instance = new ServiceConfig();
                        _instance._daoManager = DaoManager.GetInstance("SqlMapDao"); //通过名字进行实例化，该名字与Dao.Config中Context name对应
                        //CartonDao cartonDao = new CartonDao();
                        //_instance._daoManager = (DaoManager)DaoManager.GetInstance(cartonDao); ; //通过名字进行实例化，该名字与Dao.Config中Context name对应
                   
                         
                        /*
                        ConfigureHandler handler = new ConfigureHandler(ServiceConfig.Reset);
                        try
                        {
                            DaoManager.ConfigureAndWatch(handler);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }

                        _instance = new ServiceConfig();
                        _instance._daoManager = DaoManager.GetInstance();
                        */
                      }
                }
            }
            return _instance;

        }

        static public void Reset(object obj)
        {
            _instance = null; //置空_instance
        }

        public IDaoManager daoManager
        {
            get 
            {
                return _daoManager;
            }
        }
    }
}
